package com.example.leetcode.trainingcamp.week12;

/**
 * 数组nums包含从0到n的所有整数，但其中缺了一个。请编写代码找出那个缺失的整数。你有办法在O(n)时间内完成吗？
 */
public class Test17_04 {

    public int missingNumber(int[] nums) {
        if (nums.length ==1){
            return nums[0] == 0 ? 1 : 0;
        }
        int sum = 0;
        int length = nums.length;
        for (int i:nums){
            sum += i;
        }
        int doubleTotal = (length) * (length + 1);
        int differ = doubleTotal - 2 * sum;
        return differ / 2;
    }

    public static void main(String[] args) {
        Test17_04 t = new Test17_04();
        int[] nums = {0};
        System.out.println(t.missingNumber(nums));
    }
}
